My journey of C has begun [5]

  • 2020-04-01 21:27:41
  • OfStack

12. Basic data type: integer (below)

1. Output variables of various integer types

        Output different types of integers, need to use different Format qualifier . Output an integer of type unsigned int to use The % u . Output long to use % ld ; If you want to output in hexadecimal or octal form, use % lx (or % lX ) or % lo . Note: although it is okay to use uppercase or lowercase English letters for the suffixes of integer constants, they are format qualifiers You have to use lowercase ! If we want to print an integer of type short, we can add it to %d The prefix H. , that is, % hd ; In the same way, % ho and % hx (or % hX ) represents the output in octal or hexadecimal form, respectively. The prefix H. and l You can and u A combination that represents the output unsigned integer. Such as: % lu Represents an integer of type unsigned long output; % hu, Represents an integer of type output unsigned short. If your compiler supports C99, you can use it % LLD and % llu Refers to the output long long and unsigned long long respectively. Let's look at a program that outputs various types of integers:

                # include < stdio.h >

                Int main (void)
                {
                      Unsigned int UN = 3000000000;
                      Short end = 200;                          
                      Long big = 65537;

                      Printf (" UN = %u and not %d\n", UN, UN);
                      Printf ("end = %hd and %d\n", end, end);
                      Printf ("big = %ld and not %hd\n", big, big);

                      Printf (" Press ENTER to quit." );
                      Getchar ();
                      Return 0;
                }

use (link: http://cpp.ga-la.com/html/1/15/0510/12.htm) The output result of compiling and running this program is as follows:

                UN = 3000000000 and not -1294967296
                End is equal to 200 and 200
                Big is equal to 65537 and not 1
                Press ENTER to quit...

          This program shows that incorrect use of format qualifiers can result in unexpected output. First, the %d error was used as the format qualifier for the unsigned integer variable UN, resulting in a negative output. This is because my computer USES the same binary form to represent 3000000000 and -129496296 Computers know only binary . So if we use %u to tell printf to print an unsigned integer, the output is 3000000000; If we misuse %d, the output will be a negative number. However, if we change the 3000000000 in the code to 96, the output will not show an exception. Because 96 is not out of the int range.
        Then, for the second printf, the output is the same whether we use %hd or %d. That's because of C  Language standards dictate that short values are automatically converted to int values when passed to functions. Int is converted to int because int is designed to be the most efficient type of integer the computer can process. Therefore, for computers with different sizes of short and int, it is faster to convert the variable end to an int and pass it to the function. So h doesn't seem to exist. It's not. We can use %hd to see what happens when a larger integer type is truncated to a short type.
      The third printf, due to misuse of %hd, produces output of 1. This is because, if long is 32 bits, the binary form of 65537 is 0000, 0000, 0000, 0001, 0000, 0000, 0000, 0001, and the %hd command printf outputs   A value of type short, causing printf to process only the last 16 bits, resulting in output 1.
        In the previous tutorial, we said, Ensure that the number of format qualifiers is the same as the number of parameters It's our responsibility. In the same way, Ensure that the format qualifier is of the same type as the parameter type It's our responsibility! As mentioned above, incorrect use of format qualifiers can result in unexpected output!


2. Integer overflow

        First look at the following program:

                      # include < stdio.h >

                    Int main (void)
                    {
                           
                              Int I = 2147483647, j = -2147483648;
                            Unsigned int k = 4294967295, l = 0;

                            Printf ("%d %d %d %d\n", I, I +1, j, j-1);
                            Printf ("%u %u %u %u %u %u %u\n", k, k+1, k+2, l, l-1);

                              Printf (" Press ENTER to quit." );
                            Getchar ();
                            Return 0;
                      }

Using dev-c ++ to compile and run this program, the output is as follows:

                      2147483647-2147483648-2147483648, 2147483647
                    4294967295 0 1   0 4294967295
                      Press ENTER to quit...

          In this program, I plus 1 is negative, j minus 1 is positive, k plus 1 is 0, and l minus 1 is 4294967295. This is because after the addition and subtraction operation, their values exceed the representation range of their corresponding integer types, which we call this phenomenon The overflow .
      If the value of an unsigned int exceeds the upper bound, it returns 0, and then increases from 0. If it's less than 0, then it reaches the upper bound of the unsigned type, and then it goes down from the upper bound. It's like a person running around a track, making a circuit and returning to the starting point. If an int overflows, it can go negative or positive.
        For unsigned integers, they must overflow as described above, which is standard. But the standard doesn't specify what happens when a signed integer overflows. The situation described here with signed integer overflow is the most common, but in other computers, with other compilers, it can be different.

13. Identifier naming rules
 

1. Length limitation

        C89 states that the compiler should at least be able to handle it 31 Within 30 characters (including 31) Internal identifier (internal identifier); And for External identifier (external identifier), which the compiler should at least be able to handle 6 External identifiers of up to 6 characters. The so-called identifier "Is the name we give to a variable, macro, or function. For example, int num. Num in this statement is an identifier.
        The latest C99 standard states that the compiler should at least be able to handle it 63 An internal identifier of up to 63 characters (including 63); The compiler should at least be able to handle it 31 An external identifier of up to 31 characters.
        In fact, we can name the identifier with more than the maximum number of characters, but the compiler will Ignore the extra characters . That is, if we name variables with 35 characters, and the compiler can only handle variable names with 31 characters at most, then the extra 4 characters will be ignored by the compiler and only the first 31 characters will be valid. For some older compilers that can only handle identifiers of up to eight characters, the identifiers kamehameha and kamehameko are equivalent because the first eight characters are equal.


2. Available characters and combination rules

      The standard states that identifiers can only be used by Case letters . The underline (_), and Arabic numerals Composition. The first character of an identifier must be a uppercase English letter or an underscore, not a number.

                      Legal name     Illegal naming
                      Wiggles                The & # 8194; The & # 8194; The & # 8194; The & # 8194; The & # 8194; * *   $Z]      
                        Cat2                       2 the cat          
                        Hot_Tub                 Hot - Tub     
                      TaxRate              Tax rate     
                      _kcab                         Don 't      

By convention, identifiers in the operating system and C language standard libraries begin with an underscore. Therefore, we should avoid using underscores as the beginning of our own defined identifiers.
        C is a case-sensitive language, that is, star, star, star, star, and so on are mutually related different Identifier.
        We can't use The keyword and Reserved identifier To name our custom variables. For keywords and reserved identifiers, click (link: http://cpp.ga-la.com/html/1/2/0510/21.htm)


Related articles: